package com.cmwa.solution.business.register.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.register.entity.ClearRecordVo;
import com.cmwa.solution.business.register.service.SalesDataManageService;


/**
 * 	销售数据管理控制层
 * @author ex-wuh2
 *
 */
@Controller
@RequestMapping("/business/salesDataManage")
public class SalesDataManageController {
	
	@Autowired
	private SalesDataManageService salesDataManageService;
	
	private final static String JSP_PATH = "business/register/custDataMgr/";

	/**
	 * 	跳转至一级菜单展示页
	 * @return
	 */
	@RequestMapping("/salesDataManageOverView")
	public ModelAndView salesDataManageOverView(ModelAndView modelAndView) {
		String serialNo = salesDataManageService.getSequence();
		modelAndView.addObject("serialNo", serialNo);
		modelAndView.setViewName(JSP_PATH + "salesDataManageOverView");
		return modelAndView;
	}
	
	/**
	 * 	跳转至数据及清算状态页面
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/dataAndClearStatusView")
	public ModelAndView goDataAndClearStatusPageView(ModelAndView modelAndView) {
		String serialNo = "";
		try {
			serialNo = salesDataManageService.getSequence();
		} catch (Exception e) {
			e.printStackTrace();
			serialNo = "201902131335";
		}
		modelAndView.addObject("serialNo", serialNo);
		modelAndView.setViewName(JSP_PATH + "dataAndClearStatus");
		return modelAndView;
	}
	

	/**
	 * 	跳转至数据处理报告页面
	 * @return
	 */
	@RequestMapping("/dataDealReportView")
	public String goDataDealReportPageView() {
		return JSP_PATH + "dataDealReport";
	}
	
	
	/**
	 * 	查询清算状态(全步骤状态查询)
	 * @param serialNo
	 * @return
	 * @throws InterruptedException
	 */
	@RequestMapping("/queryClearStep")
	@ResponseBody
	public Map<String, Object> queryClearStatusList(String serialNo) throws InterruptedException{
		Thread.sleep(2000);
		Map<String,Object> map = new HashMap<String,Object>();
		List<ClearRecordVo> groupList = salesDataManageService.queryClearRecordList(serialNo);
		List<ClearRecordVo> docArchiveList = salesDataManageService.queryFileInfoList(serialNo, "10001");
		List<ClearRecordVo> docExportList = salesDataManageService.queryFileInfoList(serialNo, "10004");
		ClearRecordVo clearVo = salesDataManageService.querySuccessTime(serialNo);
		map.put("groupList",groupList);
		map.put("docArchiveList",docArchiveList);
		map.put("docExportList",docExportList);
		map.put("clearVo", clearVo);
		return map;
	}
	
	
	/**
	 * 	查询清算异常及基本信息
	 * @param sp
	 * @return
	 */
	@RequestMapping("/queryClearException")
	@ResponseBody
	public Map<String,Object> queryClearExceptionInfo(SearchParam sp) {
		Map<String,Object> map = new HashMap<String,Object>();
		ClearRecordVo clearVo = salesDataManageService.queryClearExceptionRecord(sp);
		List<ClearRecordVo> exceptionList = salesDataManageService.queryExceptionList(sp);
		map.put("clearVo",clearVo);
		map.put("exceptionList", exceptionList);
		return map;
	}
	
	/**
	 * 	模拟清算步骤状态写入数据库
	 * @param serialNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/makeData")
	@ResponseBody
	public Map<String,String> makeData(String serialNo) throws Exception {
		Map<String,String> map = new HashMap<String,String>();
		salesDataManageService.truncateData();
		List<ClearRecordVo> list1 = new ArrayList<ClearRecordVo>();
		List<ClearRecordVo> list2 = new ArrayList<ClearRecordVo>();
		List<ClearRecordVo> list3 = new ArrayList<ClearRecordVo>();
		List<ClearRecordVo> list4 = new ArrayList<ClearRecordVo>();
		List<ClearRecordVo> list5 = new ArrayList<ClearRecordVo>();
		List<ClearRecordVo> list6 = new ArrayList<ClearRecordVo>();
		List<ClearRecordVo> list7 = new ArrayList<ClearRecordVo>();
		for(int i = 0;i<3;i++) {
			ClearRecordVo vo = new ClearRecordVo();
			vo.setClearRecordId(serialNo);
			vo.setClearstepCode("10001");
			vo.setResultCode("0000");
			/*vo.setResultMsg("通过,"+ "数据"+  (i + 1) + "条");*/
			if(i == 0) {
				vo.setClearstepDesc("索引文件");
				vo.setClearStatus("I");
				vo.setUpdateId("0");
				vo.setResultMsg("通过,"+ "数据"+  "5" + "条");
			}else if(i == 1) {
				vo.setClearstepDesc("账户申请");
				vo.setClearStatus("I");
				vo.setUpdateId("1");
				vo.setResultMsg("通过,"+ "数据"+  "12" + "条");
			}else if(i == 2) {
				vo.setClearstepDesc("交易申请");
				vo.setClearStatus("I");
				vo.setUpdateId("2");
				vo.setResultMsg("通过,"+ "数据"+  "8" + "条");
			}
			list1.add(vo);
		}
		
		for(int i = 0;i<2;i++) {
			ClearRecordVo vo = new ClearRecordVo();
			vo.setClearRecordId(serialNo);
			vo.setClearstepCode("10001");
			vo.setResultCode("0000");
			if(i == 0) {
				vo.setClearstepDesc("非居民涉税信息");
				/*vo.setResultMsg("通过,"+ "数据"+  i + "条");*/
				vo.setResultMsg("通过,"+ "数据"+  "15" + "条");
				vo.setClearStatus("S");
				vo.setUpdateId("3");
			}else if(i == 1) {
				vo.setClearstepCode("10002");
				vo.setClearstepDesc("清算开始中");
				vo.setClearStatus("I");
			}
			list2.add(vo);
		}
		
		for(int i = 0;i<2;i++) {
			ClearRecordVo vo = new ClearRecordVo();
			vo.setClearRecordId(serialNo);
			if(i == 0) {
				vo.setClearstepCode("10002");
				vo.setClearstepDesc("清算开始完成");
				vo.setClearStatus("S");
			}else if(i == 1) {
				vo.setClearstepCode("10003");
				vo.setClearstepDesc("清算结束中");
				vo.setClearStatus("I");
			}
			list3.add(vo);
		}
		for(int i =0;i<2;i++) {
			ClearRecordVo vo = new ClearRecordVo();
			vo.setClearRecordId(serialNo);
			if(i == 0) {
				vo.setClearstepCode("10003");
				vo.setClearstepDesc("清算结束完成");
				vo.setClearStatus("S");
			}else if (i == 1) {
				vo.setClearstepCode("10004");
				vo.setClearstepDesc("索引文件");
				vo.setResultMsg("已导出,"+ "数据"+ "13" + "条");
				vo.setClearStatus("I");
				vo.setUpdateId("0");
			}
			list4.add(vo);
		}
		for(int i = 0;i<4;i++) {
			ClearRecordVo vo = new ClearRecordVo();
			vo.setClearRecordId(serialNo);
			vo.setClearstepCode("10004");
			vo.setResultCode("0000");
			/*vo.setResultMsg("已导出,"+ "数据"+  i + "条");*/
			if(i == 0) {
				vo.setClearstepDesc("账户回报");
				vo.setClearStatus("I");
				vo.setUpdateId("1");
				vo.setResultMsg("已导出,"+ "数据"+  "9" + "条");
			}else if(i == 1) {
				vo.setClearstepDesc("交易回报");
				vo.setClearStatus("I");
				vo.setUpdateId("2");
				vo.setResultMsg("已导出,"+ "数据"+  "9" + "条");
			}else if(i == 2) {
				vo.setClearstepDesc("份额对账");
				vo.setClearStatus("I");
				vo.setUpdateId("3");
				vo.setResultMsg("已导出,"+ "数据"+  "4" + "条");
			}else if(i == 3) {
				vo.setClearstepDesc("基金分红");
				vo.setClearStatus("I");
				vo.setUpdateId("4");
				vo.setResultMsg("已导出,"+ "数据"+  "7" + "条");
			}
			list5.add(vo);
		}
		for(int i = 0;i<2;i++) {
			ClearRecordVo vo = new ClearRecordVo();
			vo.setClearRecordId(serialNo);
			vo.setClearstepCode("10004");
			vo.setResultCode("0000");
			vo.setClearstepDesc("非居民涉税信息确认");
			
			if(i == 1) {
				vo.setClearstepCode("10005");
				vo.setClearstepDesc("日切中");
				vo.setClearStatus("I");
			}else {
				vo.setClearStatus("S");
				vo.setResultMsg("已导出,"+ "数据"+  "15" + "条");
				vo.setUpdateId("5");
			}
			list6.add(vo);
		}
		ClearRecordVo vo = new ClearRecordVo();
		vo.setClearRecordId(serialNo);
		vo.setClearstepCode("10005");
		vo.setResultCode("0000");
		vo.setClearstepDesc("日切完成");
		vo.setClearStatus("S");
		list7.add(vo);
		
		salesDataManageService.batchInsertRecordVo(list1);
		salesDataManageService.batchInsertRecordVo(list2);
		/*Thread.sleep(5000);
		salesDataManageService.batchInsertRecordVo(list3);
		Thread.sleep(5000);
		salesDataManageService.batchInsertRecordVo(list4);
		Thread.sleep(5000);
		salesDataManageService.batchInsertRecordVo(list5);
		Thread.sleep(5000);
		salesDataManageService.batchInsertRecordVo(list6);
		Thread.sleep(5000);
		salesDataManageService.batchInsertRecordVo(list7);
		map.put("0000", "成功");*/
		return map;
	}
}
